package com.gitee.jmliu1983.finacehelper.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
//import com.ruoyi.common.log.annotation.Log;
//import com.ruoyi.common.log.enums.BusinessType;
//import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.gitee.jmliu1983.finacehelper.domain.NvFundOpenFundInfoEm;
import com.gitee.jmliu1983.finacehelper.domain.Product;
import com.gitee.jmliu1983.finacehelper.service.INvFundOpenFundInfoEmService;
import com.gitee.jmliu1983.finacehelper.service.IProductService;
import com.ruoyi.common.annotation.Log;
//import com.ruoyi.common.core.web.controller.BaseController;
//import com.ruoyi.common.core.web.domain.AjaxResult;
//import com.ruoyi.common.core.utils.poi.ExcelUtil;
//import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 每日基金净值Controller
 * 
 * @author ruoyi
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/product_daily_netvalue")
@PreAuthorize("@ss.hasRole('finace')")
public class NvFundOpenFundInfoEmController extends BaseController {
	@Autowired
	private INvFundOpenFundInfoEmService nvFundOpenFundInfoEmService;

	@Autowired
	private IProductService productService;

	/**
	 * 查询每日基金净值列表
	 */
//    @RequiresPermissions("product_daily_netvalue:product_daily_netvalue:list")
	@GetMapping("/list")
	public TableDataInfo list(NvFundOpenFundInfoEm nvFundOpenFundInfoEm) {
		startPage();
//		List<JSONObject> listJ = new ArrayList<JSONObject>();
//		List<Product> products = productService.selectProductList(null);
		List<NvFundOpenFundInfoEm> list = nvFundOpenFundInfoEmService
				.selectNvFundOpenFundInfoEmList(nvFundOpenFundInfoEm);
//		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
//			NvFundOpenFundInfoEm nvFundOpenFundInfoEm2 = (NvFundOpenFundInfoEm) iterator.next();
//			String prodId = nvFundOpenFundInfoEm.getProductId();
//			JSONObject jObj = JSONObject.from(nvFundOpenFundInfoEm2);
//			List<Product> foundItem = products.stream().filter(item -> item.getProductId().equals(prodId))
//					.collect(Collectors.toList());
//			if (foundItem != null && foundItem.size() == 1) {
//				String prodName = foundItem.get(0).getProductName();
//				jObj.put("productName", prodName);
//			}
//			listJ.add(jObj);
//		}

		return getDataTable(list);
	}

	/**
	 * 导出每日基金净值列表
	 */
//    @RequiresPermissions("product_daily_netvalue:product_daily_netvalue:export")
	@Log(title = "每日基金净值", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, NvFundOpenFundInfoEm nvFundOpenFundInfoEm) {
		List<NvFundOpenFundInfoEm> list = nvFundOpenFundInfoEmService
				.selectNvFundOpenFundInfoEmList(nvFundOpenFundInfoEm);
		ExcelUtil<NvFundOpenFundInfoEm> util = new ExcelUtil<NvFundOpenFundInfoEm>(NvFundOpenFundInfoEm.class);
		util.exportExcel(response, list, "每日基金净值数据");
	}

	/**
	 * 获取每日基金净值详细信息
	 */
//    @RequiresPermissions("product_daily_netvalue:product_daily_netvalue:query")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return success(nvFundOpenFundInfoEmService.selectNvFundOpenFundInfoEmById(id));
	}

	/**
	 * 新增每日基金净值
	 */
//    @RequiresPermissions("product_daily_netvalue:product_daily_netvalue:add")
	@Log(title = "每日基金净值", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody NvFundOpenFundInfoEm nvFundOpenFundInfoEm) {
		return toAjax(nvFundOpenFundInfoEmService.insertNvFundOpenFundInfoEm(nvFundOpenFundInfoEm));
	}

	/**
	 * 修改每日基金净值
	 */
//    @RequiresPermissions("product_daily_netvalue:product_daily_netvalue:edit")
	@Log(title = "每日基金净值", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody NvFundOpenFundInfoEm nvFundOpenFundInfoEm) {
		return toAjax(nvFundOpenFundInfoEmService.updateNvFundOpenFundInfoEm(nvFundOpenFundInfoEm));
	}

	/**
	 * 删除每日基金净值
	 */
//    @RequiresPermissions("product_daily_netvalue:product_daily_netvalue:remove")
	@Log(title = "每日基金净值", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(nvFundOpenFundInfoEmService.deleteNvFundOpenFundInfoEmByIds(ids));
	}
}
